package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
     private StudentService studentService;

    //条件分页查询
    @GetMapping
     public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询，参数：{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
         return Result.success(pageResult);

    }

    @PostMapping
     public Result save(@RequestBody  Student student) {
        log.info("保存学生，参数：{}", student);
        studentService.save(student);
        return Result.success();
    }
    //根据id查询
     @GetMapping("/{id}")
     public Result getInfo(@PathVariable Integer id) {
        log.info("根据id查询，参数：{}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    //修改学生信息
     @PutMapping
     public Result update(@RequestBody Student student) {
        log.info("修改学生，参数：{}", student);
        studentService.update(student);
        return Result.success();
    }

    //删除学生
    @DeleteMapping( "/{ids}")
     public Result delete(@PathVariable String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        log.info("删除学生，参数：{}", idList);
        studentService.delete(idList);
        return Result.success();
    }
    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log .info("违纪处理，参数：id={},score={}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }


}
